<template>
    <div class="message" v-if="show">
        <div class="message-content" :class="{ show: isShow }">
            <div class="message-content-title">{{ title }}</div>
            <div class="message-content-inner">{{ content }}</div>
            <div class="message-content-handler">
                <div @click="cancelHandler">{{ cancelText }}</div>
                <div @click="confirmHandler">{{ confirmText }}</div>
            </div>
        </div>
    </div>
</template>



<script lang="ts" setup>
import { onMounted, ref } from "vue";

defineOptions({ name: "message" });

export interface IMsgProps {
    show?: boolean;
    title?: string;
    content: string;
    cancelText?: string;
    confirmText?: string;
}

withDefaults(defineProps<IMsgProps>(), {
    show: false,
    title: "提示",
    content: "内容",
    cancelText: "取消",
    confirmText: "确定"
})

const isShow = ref<boolean>(false);

onMounted(() => {
    setTimeout(() => {
        isShow.value = true;
    }, 300);
})


const close = () => {
    return new Promise((res) => {
        isShow.value = false;
        setTimeout(() => {
            emits("update:show", false);
            res(null)
        }, 300);
    })
}

const emits = defineEmits(["cancel", "confirm", "update:show"]);

const cancelHandler = () => {
    emits("cancel", close);
}

const confirmHandler = () => {
    emits("confirm", close);
}

</script>


<style lang="less" scoped>
.message {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 50;
    background-color: rgba(0, 0, 0, 0.5);
    display: flex;
    justify-content: center;
    align-items: center;

    &-content {
        width: 250px;
        background-color: #FFF;
        overflow: hidden;
        border-radius: 5px;
        text-align: center;
        transform: scale(0);
        transition: transform .3s;

        &.show {
            transform: scale(1);
        }

        &-title {
            font-weight: 600;
            padding: 5px 0;
        }

        &-inner {
            font-size: 14px;
            padding: 10px 10px 15px;
        }

        &-handler {
            border-top: 1px solid #cecece;
            height: 44px;
            display: flex;
            justify-content: space-between;

            div {
                flex: 1;
                height: 100%;
                line-height: 44px;

                &:active {
                    background-color: #f0f0f0f0;
                }

                &:first-child {
                    border-right: 1px solid #cecece;
                }
            }
        }
    }
}
</style>